//
//  WFSavingsMethodDetailListCollectionViewCell.m
//  WeiFang
//
//  Created by WeiFang on 2016/11/21.
//  Copyright © 2020年 WeiFang. All rights reserved.
//

#import "WFSavingsMethodDetailListCollectionViewCell.h"

@implementation WFSavingsMethodDetailListCollectionViewCell

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
}

- (void)setData:(id)data delegate:(id)delegate {

    if ([data isKindOfClass:[NSDictionary class]]) {
        if ([data[@"data"] isKindOfClass:[WFSavingsMethodDetailListCollectionViewCellModel class]]) {
            WFSavingsMethodDetailListCollectionViewCellModel *model = data[@"data"];
            _titleLabel.text = model.title;
            
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
            
            NSString *dateStr = [self getTimeWithDateString:kUserManager.userManager.startDate days:model.index];
            NSDate *date1 = [dateFormatter dateFromString:dateStr];
            //设置格式：zzz表示时区
            [dateFormatter setDateFormat:@"yyyyMMdd"];
            //NSDate转NSString
            NSString *time1 = [dateFormatter stringFromDate:date1];
            //NSDate转NSString
            NSString *time2 = [dateFormatter stringFromDate:[NSDate date]];

            if (time1.integerValue > time2.integerValue) {
                _titleLabel.backgroundColor = UIColorFromHex(0xFFFFFF);
                _titleLabel.textColor = UIColorFromHex(0x333333);
            } else {
                _titleLabel.backgroundColor = UIColorFromHex(0xFFD300);
                _titleLabel.textColor = UIColorFromHex(0x000000);
            }
        }
    }
}

- (NSString *)getTimeWithDateString:(NSString *)dateString days:(NSInteger)days {
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];

    NSDate *date = [dateFormatter dateFromString:dateString];

    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:days];

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *resultDate = [calendar dateByAddingComponents:comps toDate:date options:0];
    [dateFormatter setDateFormat:@"yyyy.MM.dd"];
    NSString *resultString = [dateFormatter stringFromDate:resultDate];
    NSLog(@"%@的%ld天之后的日期为%@", dateString, days, resultString);
    return resultString;
}

@end




#pragma mark - Model

@implementation WFSavingsMethodDetailListCollectionViewCellModel

+ (WFSavingsMethodDetailListCollectionViewCellModel *)ittemModelWithTitle:(NSString *)title index:(NSInteger)index {
    WFSavingsMethodDetailListCollectionViewCellModel *model = [WFSavingsMethodDetailListCollectionViewCellModel new];
    model.title = kSafeString(title);
    model.index = index;
    return model;
}

@end
